<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="100px" class="user-info-form">
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="用户昵称" prop="nickName">
          <el-input 
            v-model="user.nickName" 
            maxlength="30"
            placeholder="请输入用户昵称" 
            prefix-icon="el-icon-user"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="性别">
          <el-radio-group v-model="user.sex">
            <el-radio label="0">男</el-radio>
            <el-radio label="1">女</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-col>
    </el-row>
    
    <el-row :gutter="20">
      <el-col :span="12">
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input 
            v-model="user.phonenumber" 
            maxlength="11" 
            placeholder="请输入手机号码" 
            prefix-icon="el-icon-mobile-phone"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="邮箱" prop="email">
          <el-input 
            v-model="user.email" 
            maxlength="50" 
            placeholder="请输入邮箱" 
            prefix-icon="el-icon-message"
          />
        </el-form-item>
      </el-col>
    </el-row>
    
    <el-divider content-position="center">个人简介</el-divider>
    
    <el-form-item label="个人简介">
      <el-input 
        v-model="user.remark" 
        type="textarea" 
        :rows="4" 
        placeholder="请输入个人简介（选填）"
      />
    </el-form-item>
    
    <el-form-item class="form-footer">
      <el-button type="primary" @click="submit" icon="el-icon-check">保存信息</el-button>
      <el-button @click="close" icon="el-icon-close">取消修改</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile } from "@/api/system/user";

export default {
  props: {
    user: {
      type: Object
    }
  },
  data() {
    return {
      // 表单校验
      rules: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "邮箱地址不能为空", trigger: "blur" },
          {
            type: "email",
            message: "请输入正确的邮箱地址",
            trigger: ["blur", "change"]
          }
        ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          updateUserProfile(this.user).then(response => {
            this.$modal.msgSuccess("个人信息修改成功");
          });
        }
      });
    },
    close() {
      this.$tab.closePage();
    }
  }
};
</script>

<style lang="scss" scoped>
.user-info-form {
  padding: 10px 20px;
  
  .el-input, .el-select, .el-date-editor {
    width: 100%;
    
    &.is-focus {
      box-shadow: 0 0 0 2px rgba(64, 158, 255, 0.2);
    }
  }
  
  .el-divider {
    margin: 20px 0;
    
    .el-divider__text {
      color: #606266;
      font-size: 15px;
      background-color: #fff;
    }
  }
  
  .el-form-item__label {
    font-weight: 500;
  }
  
  .form-footer {
    text-align: center;
    margin-top: 30px;
    
    .el-button {
      padding: 10px 20px;
      transition: all 0.3s;
      
      &:hover {
        transform: translateY(-2px);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
      }
    }
  }
}
</style>
